package cn.car.biz.carAccess.eventListener;

import cn.car.biz.carAccess.event.VehicleRecordPutGovEvent;
import cn.car.domains.ctx.PlateDetectedContext;
import cn.car.domains.dto.BaseResponse;
import cn.car.domains.entity.VehicleEntryExitLedgerInfoEntity;
import cn.car.domains.enums.InoutTypeEnum;
import cn.car.domains.enums.UploadGovStatusEnum;
import cn.car.domains.enums.VehicleTypeEnum;
import cn.car.service.remote.TjRemoteService;
import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class VehicleRecordPutGovListener implements ApplicationListener<VehicleRecordPutGovEvent> {


    @Resource
    private TjRemoteService tjRemoteService;

    @Override
    public void onApplicationEvent(VehicleRecordPutGovEvent event) {
        PlateDetectedContext plateDetectedContext = event.getPlateDetectedContext();


        if (!plateDetectedContext.getVehicleCommonInfoEntity().getVehicleType().equals(VehicleTypeEnum.ENTRY_EXIT.name())) {
            return;
        }

        try {
            BaseResponse baseResponse = tjRemoteService.putVehiclePassRecords(plateDetectedContext.getVehicleCommonInfoEntity(),
                    plateDetectedContext.getVehicleCommonLedgerInfoEntity(),
                    plateDetectedContext.getVehicleEntryExitInfoEntity(),
                    plateDetectedContext.getVehicleEntryExitLedgerInfoEntity(),
                    InoutTypeEnum.valueOf(plateDetectedContext.getInOutType()));

            Long ledgerId = plateDetectedContext.getVehicleEntryExitLedgerInfoEntity().getId();
            InoutTypeEnum inoutType = InoutTypeEnum.valueOf(plateDetectedContext.getInOutType());

            VehicleEntryExitLedgerInfoEntity updateEntity = new VehicleEntryExitLedgerInfoEntity();
            updateEntity.setId(ledgerId);

            if ("200".equals(baseResponse.getCode()) && baseResponse.getMsg().contains("成功")
                || baseResponse.getMsg().contains("车辆进出流水号") && baseResponse.getMsg().contains("已存在")) {
                if (inoutType == InoutTypeEnum.in) {
                    updateEntity.setEntryUploadStatus(UploadGovStatusEnum.SUCCESS.name());
                } else {
                    updateEntity.setExitUploadStatus(UploadGovStatusEnum.SUCCESS.name());
                }
                log.info("[GOV_TJ] remoteService success, 推送台账数据成功, ledgerId:{}, baseResponse:{}",ledgerId, JSONObject.toJSONString(baseResponse));
            } else {

                log.error("[GOV_TJ] remoteService fail, error 推送台账数据错误, ledgerId:{}, baseResponse:{}", ledgerId, JSONObject.toJSONString(baseResponse));
                if (inoutType == InoutTypeEnum.in) {
                    updateEntity.setEntryUploadStatus(UploadGovStatusEnum.FAIL.name());
                } else {
                    updateEntity.setExitUploadStatus(UploadGovStatusEnum.FAIL.name());
                }
            }
        } catch (Exception e) {
            log.error("[GOV_TJ] remoteService carAccessException,  推送台账数据异常:", e);
        }


    }

    @Override
    public boolean supportsAsyncExecution() {
        return true;
    }
}
